package wedaex.helper;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.util.WebUtils;

import wedaex.content.ContentExtractorSet;
import wedaex.content.ContentSource;
import wedaex.content.Page;

public class SessionUtils {
	public static final String PROXY_PAGE_SESSION = "proxyPageSession";
	public static final String CONTENT_EXTRACTOR = "contentExtractorSet";

	public static Page getPage(HttpServletRequest request) {
		return (Page) WebUtils.getSessionAttribute(request, PROXY_PAGE_SESSION);
	}
	
	public static void setPage(Page p, HttpServletRequest request) {
		WebUtils.setSessionAttribute(request, PROXY_PAGE_SESSION, p);
	}

	public static ContentSource getContentSource(HttpServletRequest request) {
		Page page = getPage(request);
		if(page == null) return null;
		return page.getSource();
	}

	public static ContentExtractorSet getContentExtractorSet(HttpServletRequest request) {
		if(WebUtils.getSessionAttribute(request, CONTENT_EXTRACTOR) == null) {
			setContentExtractorSet(new ContentExtractorSet(), request);
		}
		return (ContentExtractorSet) WebUtils.getSessionAttribute(request, CONTENT_EXTRACTOR);
	}
	
	public static void setContentExtractorSet(ContentExtractorSet set, HttpServletRequest request) {
		WebUtils.setSessionAttribute(request, CONTENT_EXTRACTOR, set);
	}
}
